summaryrefslogtreecommitdiff
path: root/src/network.h
blob: 50906a8667d674197cd54614bd48122bfcacf4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef NEURAL_NETWORK_H
#define NEURAL_NETWORK_H

#include "neural/node.h"
#include "neural/column.h"
#include "neural/container.h"
#include "neural/row.h"
#include "neural/neuron.h"

#include <bu/string.h>
#include <bu/exceptionbase.h>

namespace Neural
{
	template<typename sigtype>
	class Network
	{
	public:
		Network() :
			pRoot( 0 )
		{
		}

		virtual ~Network()
		{
			delete pRoot;
		}

		static Network<sigtype> *fromStr( const Bu::String &sCode )
		{
			Network<sigtype> *pNet = new Network<sigtype>();
            Bu::String::const_iterator i = sCode.begin();
            pNet->pRoot = parseNode( i );
			return pNet;
		}

        Node<sigtype> *getRoot()
        {
            return pRoot;
        }

    private:
        // Parser code
        enum TokenType
        {
            tokRow,
            tokColumn,
            tokNeuron,
            tokOpenParen,
            tokCloseParen,
            tokNumber,
            tokComma,
            tokEndOfInput
        };

        static TokenType nextToken( Bu::String::const_iterator &i,
                int32_t &iValue )
        {
            Bu::String sTmp;
            for(; i; i++ )
            {
                switch( *i )
                {
                    case ' ':
                    case '\t':
                    case '\n':
                    case '\r':
                        continue;

                    case ',':
                        i++;
                        //Bu::println("Token: tokComma");
                        return tokComma;

                    case '(':
                        i++;
                        //Bu::println("Token: tokOpenParen");
                        return tokOpenParen;

                    case ')':
                        i++;
                        //Bu::println("Token: tokCloseParen");
                        return tokCloseParen;

                    default:
                        if( (*i >= 'a' && *i <= 'z') ||
                            (*i >= 'A' && *i <= 'Z') )
                        {
                            for(; i; i++ )
                            {
                                if( (*i >= 'a' && *i <= 'z') ||
                                    (*i >= 'A' && *i <= 'Z') )
                                {
                                    sTmp += *i;
                                }
                                else
                                {
                                    //Bu::println("Read: '%1'").arg( sTmp );
                                    sTmp = sTmp.toLower();
                                    if( sTmp == "row" )
                                        return tokRow;
                                    if( sTmp == "column" )
                                        return tokColumn;
                                    if( sTmp == "neuron" )
                                        return tokNeuron;
                                    throw Bu::ExceptionBase( Bu::String(
                                        "Invalid token discovered in input: %1"
                                        ).arg( sTmp ).end().getStr() );

                                }
                            }
                        }
                        else if( *i >= '0' && *i <= '9' )
                        {
                            for(; i; i++ )
                            {
                                if( *i >= '0' && *i <= '9' )
                                {
                                    sTmp += *i;
                                }
                                else
                                {
                                    //Bu::println("Read: '%1'").arg( sTmp );
                                    iValue = strtol( sTmp.getStr(), NULL, 10 );
                                    return tokNumber;
                                }
                            }
                        }
                        break;
                }
            }
            return tokEndOfInput;
        }

        static Node<sigtype> *parseNode( Bu::String::const_iterator &i )
        {
            int iValue;
            TokenType eTok = nextToken( i, iValue );
            Node<sigtype> *pNode = NULL;
            switch( eTok )
            {
                case tokColumn:
                    pNode = new Column<sigtype>();
                    parseNodeParams( (Column<sigtype> *)pNode, i );
                    break;

                case tokRow:
                    pNode = new Row<sigtype>();
                    parseNodeParams( (Row<sigtype> *)pNode, i );
                    break;

                case tokNeuron:
                    pNode = new Neuron<sigtype>();
                    break;

                default:
                    throw Bu::ExceptionBase(
                         "Unexpecetd token parsing neural net specifacion.");
            }
            return pNode;
        }

        static void parseNodeParams( Container<sigtype> *pParent,
                Bu::String::const_iterator &i )
        {
            int iValue;
            TokenType eTok = nextToken( i, iValue );
            if( eTok != tokOpenParen )
            {
                throw Bu::ExceptionBase(
                    "Unexpected token, expected open paren."
                    );
            }
            for(;;)
            {
                eTok = nextToken( i, iValue );
                switch( eTok )
                {
                    case tokColumn:
                        {
                            Column<sigtype> *pNode = new Column<sigtype>();
                            parseNodeParams( pNode, i );
                            pParent->addNode( pNode );
                        }
                        break;

                    case tokRow:
                        {
                            Row<sigtype> *pNode = new Row<sigtype>();
                            parseNodeParams( pNode, i );
                            pParent->addNode( pNode );
                        }
                        break;

                    case tokNeuron:
                        pParent->addNode( new Neuron<sigtype>() );
                        break;

                    case tokNumber:
                        for( int j = 0; j < iValue; j++ )
                        {
                            pParent->addNode( new Neuron<sigtype>() );
                        }
                        break;

                    default:
                        throw Bu::ExceptionBase(
                             "Unexpecetd token parsing neural net specifacion.");
                }

                eTok = nextToken( i, iValue );
                if( eTok == tokCloseParen )
                    return;
                else if( eTok == tokComma )
                    continue;
                else
                    throw new Bu::ExceptionBase(
                        "Unexpected token parsing neural net specification,"
                        " expected comma or close paren.");
            }

        }

	private:
		Node<sigtype> *pRoot;
	};
};

#endif